'''
Author: your name
Date: 2020-11-09 09:32:50
LastEditTime: 2020-11-09 14:32:49
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \python-study\djangoS\app01\app01\Views\MutilEntity\index.py
'''
from django.http import request
from django.shortcuts import HttpResponse, render
from Models.models import Book
from Models.models import Author1
from Models.models import Publish1


class mutilEntity():
    def add_book(request):
        #  获取出版社对象
        # pub_obj = Publish1.objects.filter(pk=1).first()
        # #  给书籍的出版社属性publish传出版社对象
        # book = Book.objects.create(title="菜鸟教程", price=200, pub_date="2010-10-10", publish=pub_obj)
        # print(book, type(book))
        # return HttpResponse(book)
        #  获取出版社对象
        # pub_obj = Publish1.objects.filter(pk=1).first()
        # #  获取出版社对象的id
        # pk = pub_obj.pk
        # #  给书籍的关联出版社字段 publish_id 传出版社对象的id
        # book = Book.objects.create(title="冲灵剑法", price=100, pub_date="2004-04-04", publish_id=pk)
        # print(book, type(book))
        # return HttpResponse(book)
        #  获取作者对象
        # chong = Author1.objects.filter(name="令狐冲").first()
        # ying = Author1.objects.filter(name="任盈盈").first()
        # #  获取书籍对象
        # book = Book.objects.filter(title="菜鸟教程").first()
        # #  给书籍对象的 authors 属性用 add 方法传作者对象
        # book.authors.add(chong, ying)
        # return HttpResponse(book)

        #  获取作者对象
        # chong = Author1.objects.filter(name="令狐冲").first()
        # #  获取作者对象的id
        # pk = chong.pk
        # #  获取书籍对象
        # book = Book.objects.filter(title="冲灵剑法").first()
        # #  给书籍对象的 authors 属性用 add 方法传作者对象的id
        # book.authors.add(pk)
        # pub = Publish1.objects.filter(name="明教出版社").first()
        # wo = Author1.objects.filter(name="任我行").first()
        # book = wo.book_set.create(title="吸星大法", price=300, pub_date="1999-9-19", publish=pub)
        # print(book, type(book))
        # return HttpResponse("ok")
        # author_obj = Author1.objects.get(id=20)
        # book_obj = Book.objects.get(id=4)
        # author_obj.book_set.remove(book_obj)
        # return HttpResponse("ok")
        #  获取出版社对象
        # pub_obj = Publish1.objects.filter(pk=3).first()
        # #  给书籍的出版社属性publish传出版社对象
        # book = Book.objects.create(title="菜鸟教程1", price=200, pub_date="2010-10-10", publish=pub_obj)
        # print(book, type(book))
        # return HttpResponse(book)
        #  获取作者对象
        # chong = Author1.objects.filter(name="令狐冲").first()
        # ying = Author1.objects.filter(name="任盈盈").first()
        # #  获取书籍对象
        # book = Book.objects.filter(title="菜鸟教程1").first()
        # #  给书籍对象的 authors 属性用 add 方法传作者对象
        # book.authors.add(chong, ying)
        # return HttpResponse(book)
        #  获取作者对象
        # chong = Author1.objects.filter(name="令狐冲").first()
        # #  获取作者对象的id
        # pk = chong.pk
        # #  获取书籍对象
        # book = Book.objects.filter(title="冲灵剑法").first()
        # #  给书籍对象的 authors 属性用 add 方法传作者对象的id
        # book.authors.add(pk)
        # 反向写入关系表中
        ying = Author1.objects.filter(name="任盈盈").first()
        book = Book.objects.filter(title="冲灵剑法").first()
        ying.book_set.add(book)
        return HttpResponse("ok")